其他
为什么说在Android中请求权限从来都不是一件简单的事情?
大家周二早上好。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 1)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
1 -> {
var allGranted = true
for (result in grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false
}
}
if (allGranted) {
takePicture()
} else {
Toast.makeText(this, "您拒绝了某项权限,无法进行拍照", Toast.LENGTH_SHORT).show()
}
}
}
}
fun takePicture() {
Toast.makeText(this, "开始拍照", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
requestPermissions()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
1 -> {
var allGranted = true
for (result in grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false
}
}
if (allGranted) {
takePicture()
} else {
AlertDialog.Builder(this).apply {
setMessage("拍照功能需要您同意相机和定位权限")
setCancelable(false)
setPositiveButton("确定") { _, _ ->
requestPermissions()
}
}.show()
}
}
}
}
fun requestPermissions() {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 1)
}
fun takePicture() {
Toast.makeText(this, "开始拍照", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
requestPermissions()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
1 -> {
val denied = ArrayList<String>()
val deniedAndNeverAskAgain = ArrayList<String>()
grantResults.forEachIndexed { index, result ->
if (result != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[index])) {
denied.add(permissions[index])
} else {
deniedAndNeverAskAgain.add(permissions[index])
}
}
}
if (denied.isEmpty() && deniedAndNeverAskAgain.isEmpty()) {
takePicture()
} else {
if (denied.isNotEmpty()) {
AlertDialog.Builder(this).apply {
setMessage("拍照功能需要您同意相册和定位权限")
setCancelable(false)
setPositiveButton("确定") { _, _ ->
requestPermissions()
}
}.show()
} else {
AlertDialog.Builder(this).apply {
setMessage("您需要去设置当中同意相册和定位权限")
setCancelable(false)
setPositiveButton("确定") { _, _ ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivityForResult(intent, 1)
}
}.show()
}
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
1 -> {
requestPermissions()
}
}
}
fun requestPermissions() {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION), 1)
}
fun takePicture() {
Toast.makeText(this, "开始拍照", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
PermissionX.init(this)
.permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
.onExplainRequestReason { scope, deniedList ->
val message = "拍照功能需要您同意相册和定位权限"
val ok = "确定"
scope.showRequestReasonDialog(deniedList, message, ok)
}
.onForwardToSettings { scope, deniedList ->
val message = "您需要去设置当中同意相册和定位权限"
val ok = "确定"
scope.showForwardToSettingsDialog(deniedList, message, ok)
}
.request { _, _, _ ->
takePicture()
}
}
fun takePicture() {
Toast.makeText(this, "开始拍照", Toast.LENGTH_SHORT).show()
}
}
...
implementation 'com.permissionx.guolindev:permissionx:1.3.1'
}
https://github.com/guolindev/PermissionX